package logic

import (
	"context"
	"errors"
	"math/rand"
	"shop-switcher/models/cache"
	"time"

	"shop-switcher/shoprpc/internal/svc"
	"shop-switcher/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *shoprpc.SendSmsRequest) (*shoprpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line
	code := rand.Intn(9000) + 1000

	i, _ := cache.Rdb.Get(l.ctx, "sendsms"+in.Mobile).Int()
	if i >= 1 {
		return nil, errors.New("一手机号每分钟仅允许发送1次短信验证码")
	}
	//cache.Rdb.Set(l.ctx, "sendsms"+in.Mobile, code, time.Minute*1)
	cache.Rdb.Set(l.ctx, "sendsms"+in.Mobile, code, time.Minute*3)
	return &shoprpc.SendSmsResponse{}, nil
}
